//  柯里化
function curry(fn) {
  // 传入的不是函数
  if (typeof fn !== 'function') {
    throw new Error('Not a Function！')
  }

  //  fn.length 就是函数参数的个数
  if (fn.length === 0) {
    return fn;
  }

  function curried(depth, args) {
    return function(newArgument) {
      if (depth - 1 === 0) {
        return fn(...args, newArgument);
      }
      return curried(depth - 1, [...args, newArgument]);
    }
  }

  return curried(fn.length, []);
}

// Example usage:
function add(a, b) {
  return a + b;
 }
 var curriedAdd = curry(add);
 var addFive = curriedAdd(5);
 var result = [0, 1, 2, 3, 4, 5].map(addFive);
 console.log(result); // [5, 6, 7, 8, 9, 10]